<?php

namespace App\Http\Controllers\Home;

use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    // 列表页
    public function index(Request $request)
    {
        $user = User::orderBy("id","desc")->where(function($query) use($request){
            $username =$request->input('username');
            $tel =$request->input('tel');
            if($username){
                $query->where('username','like','%'.$username.'%');
            }
            if($tel){
                $query->where('tel','like','%'.$tel.'%');
            }
        })->paginate($request->input("num")?$request->input("num"):10);
        return view("home.user.list",compact("user","request"));
    }




    //用户添加页面
    public function create()
    {
        return view("home.user.add");
    }

    public function show($id){

    }

    //添加操作
    public function store(Request $request)
    {
        $user = $request->post();

        $user['username'] = htmlspecialchars($user['username']);
        $user['password'] = Hash::make($user['password']);

        $res = User::create($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    //返回修改页面
    public function edit($id)
    {
        $user = User::find($id);
        return view("home.user.edit",compact('user'));
    }

    //执行修改操作
    public function update(Request $request, $id)
    {

        $user = $request -> all();
        if($request->input('password')){
            $user['password'] = Hash::make($request->input('password'));
        }
        if(isset($user['s'])){
            unset($user['s']);
        }
        $res = User::where("id",$id)->update($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    //删除操作
    public function destroy(Request $request,$id)
    {
        $user = User::find($id);
        $res = $user->delete();
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    //批量删除
    public function delAll(Request $request){
        $res = User::destroy($request->input('ids'));
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }
}
